package keshe;

import keshe.Exception.negativeException;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class bank {
    Scanner scanner = new Scanner(System.in);
    private ArrayList<ACCOUNT> accounts=new ArrayList<>();

    private ACCOUNT LoginAccount=new ACCOUNT();


//    //银行开户
//    public void creatBankAccount() {
//        ACCOUNT account = new ACCOUNT();
//        String bankCardId = createBankCardId();
//        account.createAccount(bankCardId);
//        accounts.add(account);
//    }


    /**
     *
     * @param account 银行账户
     */
    public void creatBankAccount(ACCOUNT account){
        accounts.add(account);
    }


    //查看银行中全部账户信息
    public void Show(){
        for (int i = 0; i < accounts.size(); i++) {
            System.out.print(String.format("%10s",accounts.get(i).getUserName()));
            System.out.print(String.format("%10s",accounts.get(i).getSex()));
            System.out.print(String.format("%10",accounts.get(i).getBankCardId()));
            System.out.print(String.format("%10.2f",accounts.get(i).getMoney()));
            System.out.print(String.format("%10s",accounts.get(i).getState()));
            System.out.print(String.format("%15s",accounts.get(i).getIDcard()));
            System.out.println();
        }
    }


    //销户
    public void AccountCancellation(){
        if(LoginAccount.getIDcard()=="0000000000000"){
            System.out.println("请您进行登录");
            login();
        }
        if(LoginAccount.getMoney()!=0){
            System.out.println("您的账户内还有余额，请取出或转出再进行销户");
            System.out.println("您的余额为："+LoginAccount.getMoney());
            return;
        }
        LoginAccount.setState(false);
        System.out.println("恭喜您！销户成功");
    }


    //解除销户
    public void Termination(){
        ACCOUNT account = new ACCOUNT();
        System.out.println("请您输入您的银行账户：");
        while (true) {
            String Id=scanner.next();
            boolean flash=false;
            for (int i = 0; i < accounts.size(); i++) {
                if(Id.equals(accounts.get(i))){
                    account=accounts.get(i);
                    flash=true;
                }
            }
            if(!flash){
                System.out.println("您输入的银行账户不存在，请您重新输入：");
            }else {
                account.setState(true);
                System.out.println("恭喜您！解除成功！");
                return;
            }
        }
    }


    /**
     *
     */
    public void login() {
        System.out.println("请您输入您的银行账户Id：");
        ACCOUNT account = new ACCOUNT();
        while (true) {
            boolean flash = false;
            String bankAccountId = scanner.next();
            for (int i = 0; i < accounts.size(); i++) {
                if (bankAccountId.equals(accounts.get(i).getBankCardId())) {
                    account=accounts.get(i);
                    flash = true;
                }
            }
            if (!account.getState()){
                System.out.println("您的账户处于不可用状态，请您进行解除销户！");
                return;
            }
            if (flash) {
                break;
            } else {
                System.out.println("您输入的银行卡号在本银行不存在，请您重新输入：");
            }
        }
        System.out.println("请您输入您的银行卡密码：");
        String password = scanner.next();
        if (password.equals(account.getPassWord())) {
            if (account.getSex().equals("男")) {
                LoginAccount = account;
                System.out.println("恭喜您！" + LoginAccount.getUserName() + "先生，" + "登陆成功！");
            } else {
                LoginAccount = account;
                System.out.println("恭喜您！" + LoginAccount.getUserName() + "女士，" + "登陆成功！");
            }
        }
    }


    //退出登录
    public void LogOut(){
        LoginAccount=new ACCOUNT();
    }

    //转账
    public void Transfer(){
        if(LoginAccount.getIDcard()=="0000000000000"){
            System.out.println("请您进行登录");
            login();
        }
                System.out.println("请输入您想要转入的账户Id：");

        //检查银行账户集合中有没有银行卡号
        ACCOUNT account = new ACCOUNT();
        while (true) {
            String Id=scanner.next();
            boolean flash = false;
            for (int i = 0; i < accounts.size(); i++) {
                if (Id.equals(accounts.get(i).getBankCardId())) {
                    flash = true;
                    account=accounts.get(i);
                }
            }
            if(!flash){
                System.out.println("您要转入的账户Id不存在，请您重新输入：");
            }else {
                break;
            }
        }
        System.out.println("请输入您要转入的金额：");
        double money=scanner.nextDouble();
        LoginAccount.setMoney(account.getMoney()-money);
        account.setMoney(account.getMoney()+money);
        System.out.println("恭喜您！转账成功！");
    }

    //存款
    public void Inmoney(){
        if(LoginAccount.getIDcard()=="0000000000000"){
            System.out.println("请您进行登录");
            login();
        }
        System.out.println("输入要存入的金额");
        double money;
        money=scanner.nextDouble();
        try {
            LoginAccount.depositMoney(money);
        } catch (negativeException e) {
            System.out.println(e.getMessage());
        }
    }


    //取款
    public void Outmoney(){
        if(LoginAccount.getIDcard()=="0000000000000"){
            System.out.println("请您进行登录");
            login();
        }
        double money=scanner.nextDouble();
        try {
            LoginAccount.drawMoney(money);
        } catch (negativeException e) {
            System.out.println(e.getMessage());
        }
    }

    //查询个人信息
    public void showSelf(){
        if(LoginAccount.getIDcard()=="0000000000000"){
            System.out.println("请您进行登录");
            login();
        }
        LoginAccount.showAccount();
        LoginAccount.showInfo();
    }


    //随机银行账户
    public String createBankCardId() {
        String bankcardId = null;
        boolean flash = true;
        while (true) {
            bankcardId = "";
            Random r = new Random();
            for (int i = 0; i < 8; ++i) {
                int data = r.nextInt(10);
                bankcardId += "" + data;
            }
            //检查银行账户集合中有没有重复的银行卡号

            for (int i = 0; i < accounts.size(); i++) {
                if (bankcardId.equals(accounts.get(i).getBankCardId())) {
                    flash = false;
                }
            }
            if (flash) break;
        }
        return bankcardId;
    }


}
